import {createStore} from 'vuex'
import R from '@/router'

export default createStore({
  state: {
    loading: false,
    dialog: {},
  },
  mutations: {
    setLoading(state, bool) {
      state.loading = bool
    },
    setDialog(state, obj) {
      Object.keys(obj).forEach(v => state.dialog[v] = obj[v])
    },
    setUser(state, data) {
      localStorage.setItem('user', JSON.stringify(data))
    },
    setAuth(state, data) {
      localStorage.setItem('auth', JSON.stringify(data))
    }
  },
  getters: {
    getAuth() {
      return JSON.parse(localStorage.getItem('auth'))
    },
    getUser() {
      return JSON.parse(localStorage.getItem('user'))
    }
  },
  actions: {
    logout(state) {
      R.replace('/login')
    }
  },
  modules: {}
})
